package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Reference
    private MenuService menuService;
    /*
    * 根据用户赋予对应的菜单
    * */
    @RequestMapping("/findAll")
    public Result findAll(){
        //获取用户登录信息
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (user == null){
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
        return menuService.findAll(user.getUsername());
    }

    /*
    * 查询分页
    * */
    @RequestMapping("/selectAllByName")
    public PageResult selectAllByName(@RequestBody QueryPageBean queryPageBean){
        return menuService.selectAllByName(queryPageBean);
    }
    /*
    * 添加菜单
    * */
    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu){
        return menuService.add(menu);
    }
    /*
    * 编辑菜单
    * */
    @RequestMapping("/update")
    public Result update(@RequestBody Menu menu){
        return menuService.update(menu);
    }
    /*
    * 删除菜单
    * */
    @RequestMapping("/delete")
    @PreAuthorize("hasAuthority('MENU_DELETE')")
    public Result delete(int id){
        return menuService.delete(id);
    }

    /*
    * 查询所有主菜单
    * */
    @RequestMapping("/selectAll")
    public Result selectAll(){
        return menuService.selectAll();
    }

    /*
    * 跟剧路径回显数据
    * */
    @RequestMapping("/findByPath")
    public Result findByPath(int path){
        return menuService.findByPath(path);
    }
    /*
    * 查询所有菜单
    * */
    @RequestMapping("/findAllMenu")
    public Result findAllMenu(){
        return menuService.findAllMenu();
    }
}
